package com.yc.bran.snack.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.bran.snack.bean.CartInfo;
import com.yc.bran.snack.enums.ResultEnmu;
import com.yc.bran.snack.service.ICartInfoService;
import com.yc.bran.snack.vo.ResultVo;

@RestController
@RequestMapping("/cart")
public class CartInfoController {

	@Autowired
	private ICartInfoService cartInfoService;

	//	查询购物车
	@PostMapping("/getCartItems")
	public ResultVo getCartItems(@RequestParam Integer uid) {
		List<CartInfo> list = cartInfoService.getCartItems(uid);
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		return new ResultVo(ResultEnmu.SUCCESS, list);
	}

	//	添加购物车
	@PostMapping("/addToCart")  
	public ResultVo addToCart(@RequestParam Integer uid, @RequestParam Integer gid,
			@RequestParam Integer num, @RequestParam String gname, 
			@RequestParam String gpic, @RequestParam Double gprice) {  
		try {  
			cartInfoService.addToCart(uid, gid, num, gname, gpic, gprice);  
			return new ResultVo(ResultEnmu.SUCCESS, "添加购物车成功");
		} catch (Exception e) {  
			return new ResultVo(ResultEnmu.DATA_NULL, "添加购物车失败");
		}  
	}

	@PostMapping("/removeFromCart")  
	public ResultVo removeFromCart(@RequestParam Integer uid, @RequestParam Integer gid) {  
		try {  
			cartInfoService.removeFromCart(uid, gid);  
			return new ResultVo(ResultEnmu.SUCCESS, "删除成功");  
		} catch (IllegalArgumentException e) {  
			return new ResultVo(ResultEnmu.ERROR);
		} catch (Exception e) {  
			return new ResultVo(ResultEnmu.DATA_NULL, "删除失败");	        }  
	}  
}
